package controllers

import (
    "fmt"

    "github.com/astaxie/beego"

    "yikexing.com.cn/wyws/models"
    "yikexing.com.cn/wyws/bus"
)

// 车队controller
type FleetController struct {
    BaseController
}

// @Title 新建一个车队
// @Description 新建一个车队
// @Param   body    body    models.FleetCommand    true    "车队实体"
// @Success 200  {object} models.FleetReturnInfo
// @router / [post]
func (this *FleetController) Post() {

    var cmd = models.FleetCommand{}
    err := this.parseAndValidateCommand(cmd)
    beego.Debug(fmt.Sprintf(" in post after validate , the cmd is %s", cmd))
    // 新建
    if err != nil {
        //命令解析出错
        beego.Debug(err)
        this.returnFormatJsonData(1001, nil, err.Error())
    } else {
        dataStack := make(map[string]interface{})
        err = bus.DispatchWithDataStack(&cmd, dataStack)
        if err != nil {
            this.returnFormatJsonData(1002, nil, err.Error())
        } else {
            this.returnFormatJsonData(0, dataStack["FleetCommand"], "获取成功")
        }
    }

    this.ServeJSON()
}

// @Title 更新车队
// @Description 更新车队
// @Param    id      path    string                       true    "车队的唯一标示"
// @Param    body    body    models.FleetUpdateCommand    true    "车队的内容实体"
// @Success 200 {object} models.FleetReturnInfo
// @router /:id [put]
func (this *FleetController) Put() {

    id, err := this.GetInt(":id")
    if err != nil {
        this.returnFormatJsonData(1, nil, "检查id是否正确")
        this.ServeJSON()
        return
    }

    var cmd = models.FleetUpdateCommand{}
    cmd.Id = int64(id)
    err = this.parseAndValidateCommand(cmd)
    beego.Debug(fmt.Sprintf(" in post after validate , the cmd is %s", cmd))

    if err != nil {
        //命令解析出错
        beego.Debug(err)
        this.returnFormatJsonData(1001, nil, err.Error())
    } else {
        dataStack := make(map[string]interface{})
        err = bus.DispatchWithDataStack(&cmd, dataStack)
        if err != nil {
            this.returnFormatJsonData(1002, nil, err.Error())
        } else {
            this.returnFormatJsonData(0, dataStack["FleetUpdateCommand"], "获取成功")
        }
    }
    this.ServeJSON()
}

// @Title 车队检索
// @Description 根据检索条件查询车队信息
// @Param   body body string true "通用查询条件"
// @Success 200 {object} models.FleetReturnInfo
// @Failure 403 结果为空
// @router /search [post]
func (this *FleetController) GetAll() {

    var cmd = models.SearchFleetCommand{}
    cmd.WhereSql = this.Ctx.Input.RequestBody
    isValid := this.checkCommand(cmd)
    if isValid == nil {
        dataStack := make(map[string]interface{})
        err := bus.DispatchWithDataStack(&cmd, dataStack)
        if err == nil {
            this.returnFormatJsonData(0, dataStack["SearchFleetCommand"], "检索车队信息成功!")
        } else {
            this.returnFormatJsonData(1, nil, fmt.Sprintf("检索备车队信息失败!错误信息:%s", err))
        }
    } else {
        this.returnFormatJsonData(1001, nil, isValid.Error())
    }
    this.ServeJSON()
}

// @Title 删除车队
// @Description 删除车队
// @Param   id    path    string    true    "车队id"
// @Success 200 {object} models.ReturnInfo
// @router /:id [delete]
func (this *FleetController) Delete() {

    id, err := this.GetInt(":id")
    if err != nil {
        this.returnFormatJsonData(1, nil, "检查id是否正确")
        this.ServeJSON()
        return
    }
    var cmd = models.DeleteFleetCommand{}
    cmd.Id = int64(id)
    isValid := this.checkCommand(cmd)
    if isValid == nil {
        err := bus.Dispatch(&cmd)
        if err == nil {
            this.returnFormatJsonData(0, nil, "删除车队信息成功!")
        } else {
            this.returnFormatJsonData(1, nil, fmt.Sprintf("删除车队信息失败!错误信息:%s", err))
        }
    } else {
        this.returnFormatJsonData(1001, nil, isValid.Error())
    }
    this.ServeJSON()
}

